<template>
  <div class="loginback">
    <div class="module">
      <div class="title">高校实验室设备管理系统</div>
      <el-form ref="form" :model="forms" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" v-model="forms.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="forms.password" type="password"></el-input>
        </el-form-item>


        <el-form-item prop="role">
          <el-select placeholder="请选择角色" v-model="forms.roles">
            <el-option :value="0" label="管理员"></el-option>
            <el-option :value="1" label="实验室管理员"></el-option>
            <el-option :value="2" label="教师"></el-option>
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="login" style="width: 100%;">登录</el-button>
        <div class="tip">还没有账号？去 <span @click="register()">注册</span> </div>
      </el-form>
    </div>

  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import service from '../request/Request';
import { ElMessage } from 'element-plus';
import router from '../router';
const forms = ref({
  username: "",
  password: "",
  roles: 0
})
const form = ref(null);
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },

  ],
  roles: [
    { required: true, message: '请选择角色', trigger: 'change' },

  ]

}
onMounted(() => {

})
const login = async () => {
  form.value.validate(async (valid) => {
    console.log(valid)
    if (valid) {
      const res = await service.post("/login", forms.value,

      );
      console.log(res)
      if (res.code == 200) {
        ElMessage({
          message: "登录成功",
          type: 'success',
          plain: true,
        })
        if (res.data.role == 0) {
          router.push({
            path: "/front"
          })

        }
        if (res.data.role == 2) {
          router.push({
            path: "/sysgl"
          })

        }
        if (res.data.role == 1) {
          router.push({
            path: "/labAdminLab"
          })

        }
      }
    }
  })

}
const register = () => {
  router.push({
    path: "/register"
  })
}
</script>

<style scoped>
.loginback {
  width: 100%;
  height: 100%;
  background: url("../assets/login.png");
  display: flex;
  align-items: center;
  justify-content: center;

}

.module {
  width: 350px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.6);
  border-radius: 5px;
  box-shadow: rgb(0 0 0 / 50%) 0px 0px 10px;
}

.title {
  font-size: 20px;
  color: #000;
  margin-bottom: 20px;
  text-align: center;
}

.tip {
  text-align: right;
  margin-top: 10px;
  font-size: 15px;

}

.tip span {
  color: #409eff;
  cursor: pointer;
}
</style>
